package com.yst.web.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

public final class DateTimeUtil {

    private static final SimpleDateFormat default_format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * Merge a date parameter and a time parameter as a datetime result
     * @param date date parameter
     * @param time time parameter
     * @return  datetime result
     */
    public static Date mergeDateTime(Date date, Date time){
        String spiltChar=" ";
        String strDate=default_format.format(date);
        strDate=strDate.substring(0,strDate.indexOf(spiltChar));
        String strTime=default_format.format(time);
        strTime=strTime.substring(strTime.indexOf(spiltChar)+1);
        String result=strDate.concat(spiltChar).concat(strTime);
        try {
            return default_format.parse(result);
        }
        catch (Exception ignore){

        }
        return date;
    }

    /**
     * Compare two time,ignore date part and return milliseconds
     * @param time1
     * @param time2
     * @return
     */
    public static long compareTime(Date time1,Date time2){
        Date dateA=mergeDateTime(new Date(),time1);
        Date dateB=mergeDateTime(new Date(),time2);
        return dateB.getTime()-dateA.getTime();
    }

    /**
     * Compare two time,ignore date part, if time1 more than time2 return true,else return false
     * @param time1
     * @param time2
     * @return
     */
    public static boolean moreThan(Date time1,Date time2){
        Date dateA=mergeDateTime(new Date(),time1);
        Date dateB=mergeDateTime(new Date(),time2);
        return dateA.getTime()>dateB.getTime();
    }

    /**
     * Compare two time,ignore date part, if time1 less than time2 return true,else return false
     * @param time1
     * @param time2
     * @return
     */
    public static boolean lessThan(Date time1,Date time2){
        Date dateA=mergeDateTime(new Date(),time1);
        Date dateB=mergeDateTime(new Date(),time2);
        return dateA.getTime()<dateB.getTime();
    }

    /**
     * Compare two time,ignore date part, if time1 equal time2 return true,else return false
     * @param time1
     * @param time2
     * @return
     */
    public static boolean equal(Date time1,Date time2){
        Date dateA=mergeDateTime(new Date(),time1);
        Date dateB=mergeDateTime(new Date(),time2);
        return dateA.getTime()==dateB.getTime();
    }

    /**
     * Judge a time is in specific time range
     * @param time time
     * @param rangeBegin range begin
     * @param rangeEnd range end
     * @return if in return true,else return false
     */
    public static boolean timeInRange(Date time,Date rangeBegin,Date rangeEnd){
        long max=Math.max(rangeEnd.getTime(),rangeBegin.getTime());
        long min=Math.min(rangeBegin.getTime(),rangeEnd.getTime());
        long timestamp=time.getTime();
        return timestamp>=min&&timestamp<=max;
    }

    /**
     * Judge a time range is in another time range
     * @param timeBegin range start
     * @param timeEnd range stop
     * @param rangeBegin target time range start
     * @param rangeEnd target time range end
     * @return in return true,else return false
     */
    public static boolean rangeTimeInRange(Date timeBegin,Date timeEnd,Date rangeBegin,Date rangeEnd){
        return timeInRange(timeBegin,rangeBegin,rangeEnd)&&
                timeInRange(timeEnd,rangeBegin,rangeEnd);
    }

    /**
     * Judge a time range is in another time range
     * @param timeRange time range
     * @param target target time range
     * @return in return true,else return false
     */
    public static boolean rangeTimeInRange(TimeRange timeRange,TimeRange target){
        return rangeTimeInRange(timeRange.getBegin(),timeRange.getEnd(),target.getBegin(),target.getEnd());
    }

    /**
     * Fetch two TimeRange intersect area
     * @param timeRange1 time range 1
     * @param timeRange2 time range 2
     * @return return intersect area
     */
    public static TimeRange intersect(TimeRange timeRange1,TimeRange timeRange2){
        long min1=Math.min(timeRange1.getBegin().getTime(),timeRange1.getEnd().getTime());
        long min2=Math.min(timeRange2.getBegin().getTime(),timeRange2.getEnd().getTime());
        long max1=Math.max(timeRange1.getBegin().getTime(),timeRange1.getEnd().getTime());
        long max2=Math.max(timeRange2.getBegin().getTime(),timeRange2.getEnd().getTime());
        if (max1<min2) return null;
        if (min1>max2) return null;
        long begin=Math.max(min1,min2);
        long end=Math.min(max1,max2);
        return new TimeRange(new Date(begin),new Date(end));
    }


}
